package com.yict.csms.system.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yict.common.entity.PageEntity;
import com.yict.common.service.impl.CommonService;
import com.yict.csms.baseInfo.entity.ContractType;
import com.yict.csms.company.entity.Company;
import com.yict.csms.system.dao.impl.BudgetNumDaoImpl;
import com.yict.csms.system.entity.BudgetNum;
import com.yict.csms.system.entity.Holiday;
import com.yict.csms.system.service.IBudgetNumService;

/**
 * 
 * 
 * @author Patrick.Deng
 * 
 */
@Service
public class BudgetNumServiceImpl extends CommonService<BudgetNum, Long> implements
		IBudgetNumService {
	@Autowired
	private BudgetNumDaoImpl budgetNumDaoImpl;

	public List<BudgetNum> search(Map<String, Object> queryMap, PageEntity page) {
		StringBuilder queryString = new StringBuilder();
		String field = " order by c.createDate desc";
		pageEntity = new PageEntity();
		queryString.append("from BudgetNum as c where 1=1");
		if (queryMap != null && queryMap.size() > 0) {
			if (queryMap.get("budgetNum") != null && !"".equals(queryMap.get("budgetNum"))) {
				queryString.append(" and c.budgetNum like :budgetNum");
			}
			if (queryMap.get("budCName") != null && !"".equals(queryMap.get("budCName"))) {
				queryString.append(" and lower(c.budCName) like :budCName");
				queryMap.put("budCName", queryMap.get("budCName").toString().toLowerCase());
			}
			if (queryMap.get("budEName") != null && !"".equals(queryMap.get("budEName"))) {
				queryString.append(" and lower(c.budEName) like :budEName");
				queryMap.put("budEName", queryMap.get("budEName").toString().toLowerCase());
			}
			if (queryMap.get("budType") != null && !"".equals(queryMap.get("budType"))) {
				queryString.append(" and c.budType like :budType");
			}
		}
		if (page.getField() != null && !"".equals(page.getField()) && page.getOrder() != null
				&& !"".equals(page.getOrder())) {
			if ("budTypeName".equals(page.getField())) {
				field = " order by c.budType" + page.getOrder();
			} else {
				field = " order by c." + page.getField() + page.getOrder();
			}
		}
		queryString.append(field);

		List<BudgetNum> list = budgetNumDaoImpl.search(queryString.toString(), queryMap,
				page);
		pageEntity.setTotalRecord(budgetNumDaoImpl.getPageEntity().getTotalRecord());
		return list;
	}
	
	/**
	 * 根据id查找实体
	 * @param ids
	 * @return
	 */
	public List<BudgetNum> listByIds(Collection<Long> ids){
		if(ids == null || ids.size() == 0) return new ArrayList<BudgetNum>();
		String hql = "from BudgetNum t where numId in (:ids)";
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("ids", ids);
		List<BudgetNum> list;
		try {
			list = budgetNumDaoImpl.search(hql,map, null);
		} catch (Exception e) {
			list = new ArrayList<BudgetNum>();
		}
		return list;
	}
	
	@Override
	public LinkedHashMap<String, String> getBudgetNumSelectMap() {
		StringBuilder queryString = new StringBuilder();
		queryString.append("from BudgetNum as c order by c.createPerson");
		List<BudgetNum> list =  budgetNumDaoImpl.search(queryString.toString(),null,null);
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		if (list != null) {
			for (BudgetNum budgetNum : list) {
				map.put(budgetNum.getNumId().toString(), budgetNum.getBudgetNum());
			}
		}
		return map;
	}
	
	public LinkedHashMap<String, String> fingAllType() {
		List<String> list =  budgetNumDaoImpl.findAllType();
		LinkedHashMap<String, String> allType = new LinkedHashMap<String, String>();
		if (list != null) {
			for (String budType : list) {
				allType.put(budType, budType);
			}
		}
		return allType;
	}

	/**
	 * 查找是否存在相同的预算编号
	 * @param budgetNum
	 * @param isSave
	 * @return true-存在
	 */
	public boolean check(BudgetNum budgetNum, boolean isSave) {
		StringBuilder queryString = new StringBuilder();
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryString.append("from BudgetNum as c where 1=1 and c.budgetNum =:budgetNum ");
		queryMap.put("budgetNum", budgetNum.getBudgetNum());
		if(isSave) {
			queryString.append(" order by c.createPerson");
		} else {
			queryString.append(" and c.numId not in (:numId) order by c.createPerson");
			queryMap.put("numId", budgetNum.getNumId());
		}
		List<BudgetNum> list =  budgetNumDaoImpl.search(queryString.toString(),queryMap,null);
		if (list == null || list.size() <= 0) {
			return false;
		}
		return true;
	}
	
	public BudgetNum findById(Long id) {
		return budgetNumDaoImpl.findById(id);
	}
	
	/**
	 * 根据属性查找唯一记录
	 * @param value
	 * @param propertyName
	 * @return
	 * @throws Exception
	 */
	public BudgetNum findByUnique(Object value,String propertyName) {
		BudgetNum vo = null;
		Map<String,Object> map = new HashMap<String, Object>();
		map.put(propertyName, value);
		List<BudgetNum> list = budgetNumDaoImpl.list(map, null, 0, 0);
		if(list.size() > 1) 
			throw new RuntimeException("与期望的唯一记录不一致，找到多个记录:" + propertyName + "=" + value);
		if(list.size() == 1) vo = list.get(0);
		return vo;
	}
}
